package com.ruoyi.project.tool.swagger.UserController;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.IdUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.VerifyCodeUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Ide;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.OperatorType;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.SysLoginService;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.domain.vo.LoginVo;
import com.ruoyi.project.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/mobileUser/")
@Api(tags = "城市定位和根据城市id获取城市分类",description = "城市定位和根据城市id获取城市分类")
public class CityController extends BaseController{

    @Autowired
    private ISysCityService sysCityService;

    @Autowired
    private ISysTypeService sysTypeService;

    @Autowired
    private ISysTypeDetailService sysTypeDetailService;

    @Autowired
    private ISysSkuService sysSkuService;




    @ApiOperation("根据城市名称获取城市id")
    @PostMapping("/getByCityName")
    @Log(title = "根据城市名称获取城市id", operatorType = OperatorType.MOBILE)
    @RepeatSubmit
    @SignatureValidation
    public AjaxResult getByCityName(@RequestBody SysCity sysCity)
    {
        if(null==sysCity){
            return AjaxResult.error("参数错误");
        }
        if(sysCity.getCityName().isEmpty()){
            return AjaxResult.error("城市名称错误");
        }
        List<SysCity> list= sysCityService.getByCityName(sysCity);
        if(list.isEmpty()){
            return AjaxResult.error("暂无此城市");
        }else{
            return AjaxResult.success(list.get(0));
        }
    }

    @ApiOperation("根据城市id获取已关联的分类")
    @PostMapping("/getTypeByCityId")
    @Log(title = "根据城市id获取已关联的分类", operatorType = OperatorType.MOBILE)
    @SignatureValidation
    public AjaxResult getTypeByCityId(@RequestBody SysType sysType)
    {
        if(null==sysType){
            return AjaxResult.error("参数错误");
        }
        if(null==sysType.getCityId()){
            return AjaxResult.error("城市ID未传");
        }
        List<SysType> list= sysTypeService.getTypeByCityId(sysType);
        return AjaxResult.success(sysTypeService.buildTypeTree(list));
    }


    @ApiOperation("通过分类id获取分类详情")
    @PostMapping("/getDetailByTypeId")
    @Log(title = "通过分类id获取分类详情", operatorType = OperatorType.MOBILE)
    @SignatureValidation
    public AjaxResult getDetailByTypeId(@RequestBody SysTypeDetail sysTypeDetail)
    {
        if(null==sysTypeDetail){
            return AjaxResult.error("参数错误");
        }
        if(null==sysTypeDetail.getTypeDetailId()){
            return AjaxResult.error("分类详情id未传");
        }
        SysTypeDetail result= sysTypeDetailService.selectSysTypeDetailById(sysTypeDetail.getTypeDetailId());
        if(null!=result){
            SysSku sysSku=new SysSku();
            sysSku.setTypeDetailId(result.getTypeDetailId());
            result.setSku(sysSkuService.selectSysSkuList(sysSku));
        }

        return AjaxResult.success(result);
    }


}
